package com.cqemme.activitiservice.service.impl;

import com.cqemme.activitiservice.config.ActivitiProcessProperties;
import com.cqemme.activitiservice.utils.EmptyChecker;
import com.cqemme.activitiservice.service.ActivitiUtilService;
import com.cqemme.comm.enums.AlarmType;
import com.cqemme.comm.utils.DateUtils;
import com.cqemme.comm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.impl.persistence.entity.VariableInstance;
import org.activiti.engine.impl.persistence.entity.VariableInstanceEntity;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author nejery
 * 需要优化
 */
@Service
@Slf4j
public class ActivitiUtilServiceImpl implements ActivitiUtilService {
    @Resource
    private RuntimeService runtimeService;


    @Resource
    private ActivitiProcessProperties activitiProcessProperties;

    @Resource
    private ManagementService managementService;

    @Resource
    private HistoryService historyService;
    @Resource
    private TaskService taskService;

    @Override
    public List<Map<String, Object>> taskListBuild(List<Task> taskList) {
        List<Map<String, Object>> reList = new ArrayList<>();
        if (EmptyChecker.isEmpty(taskList)) {
            return reList;
        }


//        Set<String> instanceIds = new HashSet<>();
//        for (Task task : taskList) {
//            instanceIds.add(task.getProcessInstanceId());
//        }
//
//
//        String sql = "select * from " +
//                managementService.getTableName(VariableInstance.class) +
//                " where NAME_ = 'alarmType' and PROC_INST_ID_ in (" + StringUtils.join(instanceIds, ",") + ")";

       // managementService.create

        Map<String, Object> taskMap;
        Integer type;
        Map<String, Object> processVariables;
        for (Task ta : taskList) {
            taskMap = new HashMap<>();
            //代理人
            //taskMap.put("assignee", ta.getAssignee());
            //任务名
            taskMap.put("name", ta.getName());
            //任务简介
            //taskMap.put("description", ta.getDescription());
            //任务创建时间
            taskMap.put("createTime", DateUtils.calculateTime(ta.getCreateTime()));
            //任务过期时间
            //taskMap.put("dueDate", ta.getDueDate());
            //执行id
            //taskMap.put("executionId", ta.getExecutionId());
            //流程id
            taskMap.put("instanceId", ta.getProcessInstanceId());
            //任务id
            taskMap.put("taskId", ta.getId());
            //人物所属流程图类型

            processVariables = runtimeService.getVariables(ta.getExecutionId());
            type = (Integer) processVariables.get("alarmType");

            taskMap.put("alarmTypeCn", AlarmType.getcName(type));

            taskMap.put("avaTypeUrl", activitiProcessProperties.getUrls().get(AlarmType.geteName(type)));

            reList.add(taskMap);
        }
        return reList;
    }

    @Override
    public List<Map<String, Object>> historyTaskListBuild(List<HistoricTaskInstance> taskList) {
        Set<String> instanceIds = new HashSet<>();
        for (HistoricTaskInstance his : taskList) {
            instanceIds.add(his.getProcessInstanceId());
        }
        String sql = "select * from " +
                managementService.getTableName(HistoricVariableInstance.class) +
                " where NAME_ = 'alarmType' and PROC_INST_ID_ in (" + StringUtils.join(instanceIds, ",") + ")";
        List<HistoricVariableInstance> hisVar = historyService.createNativeHistoricVariableInstanceQuery().sql(sql).list();

        List<Map<String, Object>> finishedTaskList = new ArrayList<>();
        Integer type;
        for (HistoricTaskInstance task : taskList) {
            Map<String, Object> finishedMap = new HashMap<>();
            String instanceId = task.getProcessInstanceId();
            finishedMap.put("taskId", task.getId());
            finishedMap.put("instanceId", instanceId);
            finishedMap.put("name", task.getName());
            finishedMap.put("createTime", DateUtils.calculateTime(task.getStartTime()));
            finishedMap.put("endTime", task.getEndTime());
            for (HistoricVariableInstance historicVariableInstance : hisVar) {
                if (instanceId.equals(historicVariableInstance.getProcessInstanceId())) {
                    type = Integer.valueOf(historicVariableInstance.getValue() + "");
                    finishedMap.put("alarmTypeCn", AlarmType.getcName(type));
                    finishedMap.put("avaTypeUrl", activitiProcessProperties.getUrls().get(AlarmType.geteName(type)));
                    break;
                }
            }
            finishedTaskList.add(finishedMap);
        }
        return finishedTaskList;
    }
}
